package com.jsgt.greekfire.ui.pool.transfercoin.fragment

import android.text.TextUtils
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.fhx.core.utils.ToastUtils

import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.util.MD5Util
import com.jsgt.greekfire.dialog.BottomMenuDialog
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.viewmodel.CoinViewModel
import kotlinx.android.synthetic.main.activity_gfc_transfer.*
import kotlinx.android.synthetic.main.activity_transfer_coin1.*
import kotlinx.android.synthetic.main.frgamnet_transfer_out.*
import kotlinx.android.synthetic.main.frgamnet_transfer_out.openpool_text1
import kotlinx.android.synthetic.main.frgamnet_transfer_out.transfer_number
import kotlinx.android.synthetic.main.frgamnet_transfer_out.transfer_password
import kotlinx.android.synthetic.main.frgamnet_transfer_out.transfer_sure
import kotlinx.android.synthetic.main.frgamnt_change_into.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.EventBus.getDefault

class TransferOutFragment : BaseFragment() {

    private var bottomMenuDialog: BottomMenuDialog<String>? = null

    override fun getLayoutId(): Int = R.layout.frgamnet_transfer_out


    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }

    override fun main() {

        var mItems = resources.getStringArray(R.array.spinnerclass)
        // Create an ArrayAdapter using a simple spinner layout and languages array
        val aa = ArrayAdapter(activity!!, android.R.layout.simple_spinner_item, mItems)
        // Set layout to use when the list of choices appear
        aa.setDropDownViewResource(R.layout.layout_spinner_item)//simple_spinner_dropdown_item

        //Set Adapter to Spinner
//        Spinner_text!!.adapter = aa

//      val  cardNumber = Spinner_text.selectedItem.toString();
//
//        if (cardNumber == "GFC"){
//            EventBus.getDefault().postSticky(1) //1
//        }else{
//            EventBus.getDefault().postSticky(0) //3
//        }

        ll_type.setOnClickListener {
            showBottomMenuDialog()
        }

        transfer_sure.setOnClickListener {
            hideSoftKeyboard(activity)
            when (openpool_text1.text.toString()) {
                "GFC" -> {
                    poolIn(1);
                }
                "GFT" -> {
                    poolIn(3);
                }
                else -> {
                    showToast("请选择资产类别")
                }
            }
        }
        coinViewModel.convertLiveData.observe(this, Observer {
            showProgress(false)
            transfer_sure.isClickable = true
            activity?.finish()
        })

        coinViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
            transfer_sure.isClickable = true
        })
    }

    private fun poolIn(assetGuid: Int) {
        if (TextUtils.isEmpty(transfer_number.text.toString().trim())) {
            ToastUtils.show(activity!!, "数量不能为空")
            transfer_sure.isClickable = true
            return
        }

        if (TextUtils.isEmpty(transfer_password.text.toString().trim())) {
            ToastUtils.show(activity!!, "请输入交易密码")
            transfer_sure.isClickable = true
            return
        }

        showProgress(true)
        coinViewModel.poolIn(
            assetGuid,
            transfer_number.text.toString().trim(),
            MD5Util.getMD5(transfer_password.text.toString().trim())
        )
        transfer_sure.isClickable = false
    }

    private fun showBottomMenuDialog() {
        if (bottomMenuDialog == null) {
            bottomMenuDialog = context?.let { BottomMenuDialog(it) }
            bottomMenuDialog?.setValues(arrayOf("GFC", "GFT"))
            bottomMenuDialog?.setOnItemClickListener {
                openpool_text1.text = it
            }
        }
        if (!bottomMenuDialog!!.isShowing) {
            bottomMenuDialog!!.show()
        }
    }
}